package com.yongoe.ecy.firstNode.controller;

import com.yongoe.ecy.firstNode.entity.GraphData;
import com.yongoe.ecy.firstNode.service.FirstNodeService;
import com.yongoe.ecy.config.aop.WebLog;
import com.yongoe.ecy.firstNode.controller.vo.excel.FirstNodeExcel;
import com.yongoe.ecy.firstNode.controller.vo.req.FirstNodeReq;
import com.yongoe.ecy.firstNode.controller.vo.res.FirstNodeRes;
import com.yongoe.ecy.firstNode.convert.FirstNodeConvert;
import com.yongoe.ecy.firstNode.entity.FirstNode;
import com.yongoe.ecy.secondNode.service.SecondNodeService;
import com.yongoe.ecy.thirdNode.service.ThirdNodeService;
import com.yongoe.ecy.utils.ExcelUtils;
import com.yongoe.ecy.utils.PageUtils;
import com.yongoe.ecy.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 一级节点
 *
 * @author monad
 * @since 2024/03/10
 */
@Tag(name = "一级节点")
@RestController
@RequestMapping("/firstNode/firstnode")
public class FirstNodeController {
    @Resource
    private FirstNodeService firstNodeService;
    @Resource
    private SecondNodeService secondNodeService;
    @Resource
    private ThirdNodeService thirdNodeService;
    @Resource
    private FirstNodeConvert firstNodeConvert;

    //@WebLog
    @Operation(summary = "查询分页数据")
    @PostMapping("/page")
    public R page(@RequestBody FirstNodeReq req) {
        FirstNode entity = firstNodeConvert.req2Entity(req);
        Page<FirstNode> page = firstNodeService.getFirstNodeByPage(Page.of(req.getCurrent(), req.getSize()), entity);
        return R.success().put(new PageUtils(page));
    }

    @Operation(summary = "获得全部图谱")
    @PostMapping("/getNodeGraph/{nodeId}")
    public R getNodeGraph(@PathVariable Long nodeId) {
        List<GraphData> nodeGraph = firstNodeService.getNodeGraph(nodeId);
        System.out.println("=======================================");
        System.out.println(nodeGraph);
        System.out.println("=======================================");
        return R.success().put(nodeGraph);
    }

    @WebLog
    @Operation(summary = "新增一级节点")
    @PostMapping("/addfirstnode")
    public R addNode(@RequestBody FirstNode firstNode) {
        Long nodeID = firstNodeService.addFirstNode(firstNode);
        firstNode.setNodeId(nodeID);
        firstNodeService.save(firstNode);
        return R.success("添加成功");
    }

    @WebLog
    @Operation(summary = "修改一级节点")
    @PostMapping("/updatefirstnode")
    public R updateNode(@RequestBody FirstNode firstNode) {
        firstNodeService.updateFirstNode(firstNode);
        firstNodeService.updateById(firstNode);
        return R.success("添加成功");
    }

    @WebLog
    @Operation(summary = "删除一级节点")
    @PostMapping("/deletefirstnodes/{ids}")
    public R deleteNode(@PathVariable Long[] ids) {
        if (firstNodeService.selectChild(ids) != 0){
            return R.error("有节点存在子节点，无法删除！");
        }
        firstNodeService.deleteFirstNodes(ids);
        firstNodeService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @WebLog
    @Operation(summary = "初始化neo4j图谱")
    @PostMapping("/deleteall")
    public R deleteAllNode() {
        firstNodeService.deleteAllNode();
        firstNodeService.removeAll();
        secondNodeService.removeAll();
        thirdNodeService.removeAll();
        return R.success("初始化成功");
    }

    // @WebLog
    // @Operation(summary = "导入数据")
    // @PostMapping("/upload")
    // public R upload(MultipartFile file) {
    //     List<FirstNodeExcel> excelList = ExcelUtils.upload(file, FirstNodeExcel.class);
    //     List<FirstNode> list = firstNodeConvert.excel2EntityList(excelList);
    //     firstNodeService.saveBatch(list);
    //     return R.success("导入成功");
    // }

    // @WebLog
    // @Operation(summary = "导出数据")
    // @GetMapping("/export")
    // public void export(FirstNodeReq req, HttpServletResponse response) {
    //     FirstNode entity = firstNodeConvert.req2Entity(req);
    //     Page<FirstNode> page = firstNodeService.getFirstNodeByPage(Page.of(-1, -1), entity);
    //     List<FirstNodeExcel> list = firstNodeConvert.entity2ExcelList(page.getRecords());
    //     ExcelUtils.export(response, list, FirstNodeExcel.class);
    // }
}